--if not ( RES_DISTRIB == 3 ) then Error("Please Install Resource Distribution 2 Addon.'" ) return end

TOOL.Category = 'Asteroid Mining Tool'
TOOL.Name = '#Ice Storage Modules'
TOOL.Command = nil
TOOL.ConfigName = ''
--if (CLIENT and GetConVarNumber("RD.UseLSTab") == 1) then TOOL.Tab = "Life Support" end
if (CLIENT and GetConVarNumber("CAF_UseTab") == 1) then TOOL.Tab = "Custom Addon Framework" end

TOOL.ClientConVar['type'] = 'storage_ice'
TOOL.ClientConVar['model'] = 'models/props_wasteland/laundry_washer003.mdl'

cleanup.Register('am_store_ice_mod')

if ( CLIENT ) then
	language.Add( 'Tool_am_store_ice_mod_name', 'Life Support Resource modules' )
	language.Add( 'Tool_am_store_ice_mod_desc', 'Create Life Support Resource modules attached to any surface.' )
	language.Add( 'Tool_am_store_ice_mod_0', 'Left-Click: Spawn a Device.  Right-Click: Repair Device.' )

	language.Add( 'Undone_am_store_ice_mod', 'Ice Storage Undone' )
	language.Add( 'Cleanup_am_store_ice_mod', 'AM: Ice Storage Modules' )
	language.Add( 'Cleaned_am_store_ice_mod', 'Cleaned up all Ice Storag Modules' )
	language.Add( 'SBoxLimit_am_store_ice_mod', 'Maximum Ice Storag Modules Reached' )
end

local am_store_ice_mod = {}
if (SERVER) then
	am_store_ice_mod.storage_ice = function( ply, ent, system_type, system_class, model )
		local hash = {}
		local angles = ent:GetAngles()
		
		ent:SetAngles(Vector(0, 0, 0))
		
		local min, max = ent:WorldSpaceAABB()
		local size = max-min
		local square = (max.x - min.x) * (max.y - min.y)
		local volume = square * (max.z - min.z)
		
		ent:SetAngles(angles)
		
		Msg("Volume: "..tostring(volume).."\n")
		
		hash.size = volume/7.59
		hash.m3 = (volume/61023.7441)/10
		
		local maxhealth = math.Round(hash.size/6)
		local mass = math.Round(hash.size/40)
				

		RD.AddResource(ent, "Blue Ice", math.Round(hash.m3))
		RD.AddResource(ent, "Clear Ice", math.Round(hash.m3))
		RD.AddResource(ent, "Glacial Mass", math.Round(hash.m3))
		RD.AddResource(ent, "White Glaze", math.Round(hash.m3))
		RD.AddResource(ent, "Dark Glitter", math.Round(hash.m3))
		RD.AddResource(ent, "Glare Crust", math.Round(hash.m3))
		RD.AddResource(ent, "Gelidus", math.Round(hash.m3))
		RD.AddResource(ent, "Krystallos", math.Round(hash.m3))
		
		hash.size = math.Round(hash.size)
		
		
		--LS_RegisterEnt(ent, "Ice Storage")
		--RD.RegisterNonStorageDevice(ent, "Ice Storage")
		
		return hash, maxhealth, mass  	
	end
end

local storage_models = {
	--{ 'Huge', 'models/Spacebuild/resourcecachehuge.mdl', 'storage_ice' },
	--{ 'Massive', 'models/Spacebuild/resourcecachemassive.mdl', 'storage_ice' },
	{ 'Small Cache', 'models/ce_ls3additional/resource_cache/resource_cache_small.mdl', 'storage_ice' },
	{ 'Medium Cache', 'models/ce_ls3additional/resource_cache/resource_cache_medium.mdl', 'storage_ice' },
	{ 'Large Cache', 'models/ce_ls3additional/resource_cache/resource_cache_large.mdl', 'storage_ice' },
	{ 'Carrier Bay', 'models/Slyfo/carrierbay.mdl', 'storage_ice' },
	{ '2x Carrier Bay', 'models/Slyfo/doublecarrier.mdl', 'storage_ice' },
	{ 'Massive Bay', 'models/Spacebuild/medbridge2_fighterbay3.mdl', 'storage_ice' },
	{ 'Large Tank', 'models/props_wasteland/coolingtank02.mdl', 'storage_ice' },
	{ 'Medium Tank', 'models/props_wasteland/horizontalcoolingtank04.mdl', 'storage_ice' }
	
}

RD2_ToolRegister( TOOL, storage_models, nil, "am_store_ice_mod", 30, am_store_ice_mod )
